//
//  SequenceView.m
//  CarHome
//
//  Created by 张世琦 on 14-9-22.
//  Copyright (c) 2014年 WJN_work@163.com. All rights reserved.
//

#import "SequenceView.h"
#import "SequenceModel.h"
#import "DeliverModel.h"


NSString *const sequenceCell = @"celled";
#define CENTER 160

@implementation SequenceView

- (void)dealloc
{
    [_sequenceArray release];
    _sequenceArray = nil;
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
        _sequenceArray = [[NSMutableArray alloc] init];
        [self createButton];
        [self connect];
        UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
                                                        initWithTarget:self
                                                        action:@selector(handlePan:)];
        [self addGestureRecognizer:panGestureRecognizer];
        [panGestureRecognizer release];
    }
    return self;
}

- (void)sendUrl:(myBlock)block
{
    self.block = block;
}

#pragma mark -
#pragma mark 右侧透明button

- (void)createButton
{
    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    [_button setFrame:CGRectMake(0, 0, 80, self.frame.size.height)];
    [_button setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];
    [_button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_button];
}

- (void)buttonAction:(id)sender
{
    [self removeFromSuperview];
}

#pragma mark -
#pragma mark 滑动手势

-(void) handlePan:(UIPanGestureRecognizer*) recognizer
{
    CGPoint point = [recognizer translationInView:self];
    float x = self.center.x + point.x;
    _button.backgroundColor = [UIColor clearColor];
    if (x < CENTER) {
        x = self.center.x;
    }
    self.center = CGPointMake(x, self.center.y);
    if(recognizer.state == UIGestureRecognizerStateEnded)
    {
        [UIView animateWithDuration:1
                              delay:0.01
                            options:UIViewAnimationCurveEaseInOut
                         animations:^{
                             if (x > 250) {
                                 self.center = CGPointMake(400, self.center.y);
                             } else {
                                 self.center = CGPointMake(CENTER, self.center.y);
                                 _button.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
                             }
                         } completion:^(BOOL finished) {
                             
                         }];
    }
    [recognizer setTranslation:CGPointZero inView:self];
}

#pragma mark -
#pragma mark 创建TableView

- (void)createTableView
{
    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(80, 0, 240, self.frame.size.height - 20)];
    aView .backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    UIView *bView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 240, 44)];
    bView.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, 80, 44)];
    label.text = @"排序方式";
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(190, 0, 50, 44)];
    [button setTitle:@"关闭" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [bView addSubview:button];
    [bView addSubview:label];
    [aView addSubview:bView];
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 74, 240, [_sequenceArray count] * 40) style:UITableViewStylePlain];
    _table.dataSource = self;
    _table.delegate = self;
    [_table registerClass:[UITableViewCell class] forCellReuseIdentifier:sequenceCell];
    [aView addSubview:_table];
    [self addSubview:aView];
    
}

#pragma mark -
#pragma mark UITableViewDataSource协议

//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_sequenceArray count];
}

//重用机制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sequenceCell forIndexPath:indexPath];
    SequenceModel *model = [_sequenceArray objectAtIndex:indexPath.row];
    [cell.textLabel setText:model.name];
    return cell;
}

#pragma mark -
#pragma mark UITableViewDelegate协议

//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

//cell点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SequenceModel *sequence = [_sequenceArray objectAtIndex:indexPath.row];
    DeliverModel *model = [[DeliverModel alloc] init];
    model.value = sequence.value;
    self.block(model);
    [self removeFromSuperview];
}

#pragma mark -
#pragma mark 网络请求

- (void)connect
{
    [NetUrl connect:Day33 parmater:nil Block:^(NSData *data) {
        NSMutableDictionary *dic = (NSMutableDictionary *)data;
        NSMutableDictionary *result = [dic objectForKey:@"result"];
        NSMutableArray *metalist = [result objectForKey:@"metalist"];
        for (NSMutableDictionary *dic in metalist) {
            if ([[dic objectForKey:@"key"] isEqualToString:@"buyorder"]) {
                NSMutableArray *list = [dic objectForKey:@"list"];
                for (NSMutableDictionary *dic in list) {
                    SequenceModel *model = [[SequenceModel alloc] init];
                    model.name = [dic objectForKey:@"name"];
                    model.value = [[dic objectForKey:@"value"] intValue];
                    [_sequenceArray addObject:model];
                }
            }
        }
        [self createTableView];
        [_table reloadData];
    }];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
